#include "SpriteStarScore.h"
#include "CGlobal.h"
using namespace cocos2d;
SpriteStarScore::SpriteStarScore(void)
{
	nStarNum_=0;
	nBlackCloud_=0;
	nPlane_=0;
}

SpriteStarScore::~SpriteStarScore(void)
{
}

SpriteStarScore * SpriteStarScore::node()
{
	SpriteStarScore *pNode=new SpriteStarScore;
	if (pNode)
	{
		pNode->autorelease();
		if (pNode->init())
		{
			return pNode;
		}
	}
	return NULL;
}

bool SpriteStarScore::init()
{
	if(!CCSprite::init())
		return false;

	const std::string strArray[]={"star.png","blackcloud.png","plane.png"};
	const SCORE_TAG   scoreTag[]={TAG_SCORE_STAR,TAG_SCORE_BLACKCLOUD,TAG_SCORE_PLANE};
	if (sizeof(strArray)/sizeof(std::string)!=sizeof(scoreTag)/sizeof(SCORE_TAG) )
	{
		return false;
	}

	for (int i=0;i<sizeof(strArray)/sizeof(std::string);i++)
	{
		if (!initItem(strArray[i].c_str(),scoreTag[i]))
		{
			return false;
		}
	}
	

	return true;
}

void SpriteStarScore::addStar( int nAddStar )
{
	nStarNum_+=nAddStar;
	update(TAG_SCORE_STAR,nStarNum_);
}

void SpriteStarScore::update(SCORE_TAG scoreTag,int nNum)
{
	CCNode *pItem=getChildByTag(scoreTag);
	if (pItem)
	{
		CCLabelTTF *pLable=(CCLabelTTF *)pItem->getChildByTag(TAG_LABLE);
		if (pLable)
		{
			char buf[100]={0};
			sprintf(buf,"x %d",nNum);
			pLable->setString(buf);
		}
	}
}

void SpriteStarScore::addBlackcloud( int nBlackcloud )
{
	nBlackCloud_+=nBlackcloud;
	update(TAG_SCORE_BLACKCLOUD,nBlackCloud_);
}



void SpriteStarScore::addPlane( int nPlane )
{
	nPlane_+=nPlane;
	update(TAG_SCORE_PLANE,nPlane_);
}

bool SpriteStarScore::initItem(const char *szName, SCORE_TAG tag )
{
	CCSprite *pItem=CCSprite::node();
	CCSprite *pPic=CGlobal::SpriteWithFile(szName);
	CCLabelTTF *pLable=CCLabelTTF::labelWithString("x 0",CCSize(0,0),kCCTextAlignmentLeft,"arial",20);
	if (pItem&&pPic&&pLable)
	{
		pPic->setScale(0.3f);
		pItem->addChild(pPic,0,TAG_PIC);
		pItem->addChild(pLable,0,TAG_LABLE);
		pLable->setPosition(ccp(30,0));
		pItem->setPosition(ccp(0,-50.0f*getChildrenCount()));
		addChild(pItem,0,tag);
	
		return true;
	}
	return false;
}